//js表单验证
$(function() {
    $('#doc-vld-msg').validator({
        onValid: function(validity) {
            $(validity.field).closest('.am-form-group').find('.am-alert').hide();
        },

        onInValid: function(validity) {
            var $field = $(validity.field);
            var $group = $field.closest('.am-form-group');
            var $alert = $group.find('.am-alert');
            // 使用自定义的提示信息 或 插件内置的提示信息
            var msg = $field.data('validationMessage') || this.getValidationMessage(validity);

            if (!$alert.length) {
                $alert = $('<div class="am-alert am-alert-danger"></div>').hide().
                    appendTo($group);
            }

            $alert.html(msg).show();
        }
    });
});

//保存功能
$(function() {
    $('#doc-save-toggle').on('click', function() {
        var formValidity =  $('#doc-vld-msg').validator('isFormValid')

        if(formValidity) {
            $('#my-save').modal({
                relatedTarget: this,
                onConfirm: function (options) {
                    if ($('#seller').val() == "") {
                        alert("卖家不能为空")
                        return
                    }

                    if ($('#title').val() == "") {
                        alert("标题不能为空")
                        return
                    }

                    if ($('#serviceTime').val() == "") {
                        alert("服务时间不能为空")
                        return
                    }

                    $('#my-modal-loading').modal('open')

                    var options = {
                        url: 'othersave',
                        type: 'post',
                        dataType: 'json',
                        data: {
                            'seller': $('#seller').val(),
                            'title': $('#title').val(),
                            'serviceTime': $('#serviceTime').val(),
                            'content': $('#content').val()
                        },
                        error: function (request) {
                            $('#my-modal-loading').modal('close')
                            $('#alert-msg').html("保存失败")
                            $('#my-alert').modal('open')

                            $('#seller').val("")
                            $('#title').val("")
                            $('#serviceTime').val("")
                            $('#content').val("")
                        },
                        success: function (data) {
                            $('#my-modal-loading').modal('close')
                            if (data.code == 200) {
                                location.href = "../other/otherindex"
                            } else {
                                $('#alert-msg').html("保存失败")
                                $('#my-alert').modal('open')
                            }

                            $('#seller').val("")
                            $('#title').val("")
                            $('#serviceTime').val("")
                            $('#content').val("")
                        }
                    };
                    $.ajax(options);

                },
                // closeOnConfirm: false,
                onCancel: function () {
                }
            });
        }else{

        }
    });
});